package com.rightsidetech.http.controller.admin.store;

import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.store.StoreAwardIntegralReq;
import com.rightsidetech.system.dtos.request.store.StoreIntegralDetailReq;
import com.rightsidetech.system.dtos.request.store.StoreIntegralPageReq;
import com.rightsidetech.system.dtos.request.store.StoreIntegralRecordReq;
import com.rightsidetech.system.dtos.response.store.StoreIntegralDetailResp;
import com.rightsidetech.system.dtos.response.store.StoreIntegralRecordResp;
import com.rightsidetech.system.dtos.response.store.StoreIntegralResp;
import com.rightsidetech.system.entity.SysUserLoginInfo;
import com.rightsidetech.system.service.SysStoreIntegralService;
import com.rightsidetech.system.service.SysUserLoginInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

@Slf4j
@RestController
@RequestMapping("/admin/sys_integral")
@ShenyuSpringMvcClient(path = "/admin/sys_integral/**")
@Api(tags = "门店积分相关")
public class SysStoreIntegralController extends BaseController {

    @Resource
    private SysStoreIntegralService sysStoreIntegralService;
    @Resource
    private SysUserLoginInfoService sysUserLoginInfoService;

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    @ApiOperation("分页查询门店积分相关")
    public ResponseData<PageInfo<StoreIntegralResp>> pageIntegral(StoreIntegralPageReq req){
        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        if(loginInfo.getSysCompanyAreaId() != 0){
            req.setAreaId(loginInfo.getSysCompanyAreaId());
        }

         return sysStoreIntegralService.page(req);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/award")
    @ApiOperation("奖励门店积分")
    public ResponseData award(@RequestBody StoreAwardIntegralReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if(null == loginInfoId || null == loginName){
            return ResponseData.fail("未找到登录人");
        }

        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(loginInfoId);
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        if(loginInfo.getSysCompanyAreaId() != 0){
            req.setAreaId(loginInfo.getSysCompanyAreaId());
        }

        return sysStoreIntegralService.awardIntegral(req,loginName,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/detail")
    @ApiOperation("门店积分详情")
    public ResponseData<PageInfo<StoreIntegralDetailResp>> detail(StoreIntegralDetailReq req){
        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        if(loginInfo.getSysCompanyAreaId() != 0){
            req.setAreaId(loginInfo.getSysCompanyAreaId());
        }

         return sysStoreIntegralService.detailPage(req);
    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/record")
    @ApiOperation("积分操作记录")
    public ResponseData<PageInfo<StoreIntegralRecordResp>> record(StoreIntegralRecordReq req){
        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        if(loginInfo.getSysCompanyAreaId() != 0){
            req.setAreaId(loginInfo.getSysCompanyAreaId());
        }
        return sysStoreIntegralService.recordPage(req);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/detail_an_detail")
    @ApiOperation("详情里的详情")
    public ResponseData detailAnDetail(@ApiParam(required = true) @RequestParam("id") Long id){
        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        Long areaId = null;
        if(loginInfo.getSysCompanyAreaId() != 0){
            areaId = loginInfo.getSysCompanyAreaId();
        }
        return sysStoreIntegralService.detailAnDetail(id,areaId);
    }

}
